package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicalException;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.repository.UserInfoRepository;
import cn.wolfcode.wolf2w.service.IUserInfoService;
import cn.wolfcode.wolf2w.util.AsserUtil;
import cn.wolfcode.wolf2w.util.Consts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.UUID;

@Service
//@Transactional
public class UserInfoServiceImpl implements IUserInfoService {

    @Autowired
    private UserInfoRepository repository;

    @Autowired
    private IUserInfoRedisService userInfoRedisService;

    @Override
    public void save(UserInfo userInfo) {
        repository.save(userInfo);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public void update(UserInfo userInfo) {
        repository.save(userInfo);
    }

    @Override
    public UserInfo get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<UserInfo> list() {
        return repository.findAll();
    }

    @Override
    public boolean checkPhone(String phone) {
        //根据数据库字段phone查询用户信息
        UserInfo userInfo = repository.findByPhone(phone);
        //不为空返回true
        return userInfo != null;
    }

    @Override
    public void sendVerifyCode(String phone) {

        //生成4位数验证码
        String code = UUID.randomUUID().toString().replaceAll("-", "")
                .substring(0, 4);
        //分开业务层,每个方法实现一个功能,将验证码存到redis
        userInfoRedisService.setVerifyCode(phone, code);
        //字符串拼接
        StringBuilder sb = new StringBuilder();
        sb.append("你的验证码是:").append(code).append(",请在").append(Consts.VERIFY_CODE_VAI_TIME).append("分钟内填写");

        System.out.println(sb);

        //真实发送短信本质上是使用java 发起http请求即可
        //springmvc 提供一个工具类:RestTemplate 用于发起请求
        /*RestTemplate template = new RestTemplate();

        //参数1: 短信接口url
        String url = "https://way.jd.com/chuangxin/dxjk?mobile={1}&content=【创信】{2}&appkey={3}";
         //参数2:请求接口完之后响应数据封装到对象类型
        //参数3: 请求参数列表
        String ret = template.getForObject(url, String.class,  phone,sb.toString(), "9a4da36d3f8525112a845d6812ff9aa3");

        System.out.println(ret);
        if (!ret.contains("Success")){
            throw new LogicalException("短信发送失败");
        }*/
    }

    @Override
    public void regist(String phone, String nickname, String password, String rpassword, String verifyCode) {

        //参数校验
        //校验各参数是否为空
        AsserUtil.hasLength(phone, "手机号码不能为空");
        AsserUtil.hasLength(nickname, "昵称不能为空");
        AsserUtil.hasLength(password, "密码不能为空");
        AsserUtil.hasLength(rpassword, "确认密码不能为空");
        AsserUtil.hasLength(verifyCode, "短信验证码不能为空");

        //校验密码和确认密码相等
        AsserUtil.IsEquals(password, rpassword);

        //校验手机号正确,使用正则

        //校验手机号是唯一的
        UserInfo userInfo1 = repository.findByPhone(phone);
        if(userInfo1 != null){
            throw new LogicalException("手机错误");
        }

        
        //获取redis中的验证码
        String val = userInfoRedisService.getVerifyCode(phone);
        //校验验证码是否正确
       if(!verifyCode.equalsIgnoreCase(val)){
            throw new LogicalException("短信验证码错误");
        }

        //封装数据到对象
        UserInfo userInfo = new UserInfo();
       userInfo.setPhone(phone);
       userInfo.setCity("");
       userInfo.setEmail("");
       userInfo.setGender(0);
       userInfo.setHeadImgUrl("/images/default.jpg");
       userInfo.setInfo("");
       userInfo.setNickname(nickname);
       userInfo.setPassword(password);
       userInfo.setLevel(0);
       userInfo.setState(UserInfo.STATE_NORMAL);    //重要的参数需要自己去控制

       //int i = 1/0;
       //存入数据库
        repository.save(userInfo);
    }

    @Override
    public UserInfo getUser(String username, String password) {
        //校验请求参数
        AsserUtil.hasLength(username, "用户名不能为空");
        AsserUtil.hasLength(password, "密码不能为空");

        //通过用户名查询
        UserInfo userInfo = repository.findByPhone(username);

        //判断用户是否存在
        if (userInfo == null || !userInfo.getPassword().equals(password)){
            throw new LogicalException("用户名或密码错误");
        }
        //前端不显示密码
        userInfo.setPassword("");
            return userInfo;

    }


}
